package com.example.caffeine.controller;

import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
public class ObjectController {

    @Autowired
    private Cache<Object,Object> cache;
    @GetMapping("/api/obj/{id}")
    public Object selectById(@PathVariable("id") String id){
        //1.从本地缓存取数据
       Object obj=cache.getIfPresent(id);
       if(obj!=null){
           System.out.println("从缓存获取了数据");
           return obj;
       }

        //2.从数据库取数据
        System.out.println("从数据库查询数据");
        List<String> list= Arrays.asList("A","B","C");
        cache.put(id,list);//向缓存放数据
        return list;
    }
}
